package controller.v2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;

import service.v2.OrderService;
import service.v2.PayService;
import util.ConfigUtil;
import util.PackageUtil;

@Controller
@RequestMapping("/v2/pay")
public class AliPayController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private PayService payService;
	
	/**
	 * 支付宝统一下单接口
	 * 
	 * @param request
	 * @param response
	 * @throws Exception 
	 * @attr param payType 1 余额充值 
	 */
	@ResponseBody
	@RequestMapping("/aliPrePay")																							
	public Object aliPrePay(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id ,  
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(request.getAttribute("payType")==null){
			return PackageUtil.packageParams(403,"forbidden","",""); 
		}
		int type = (Integer)request.getAttribute("payType");
		System.out.println("请求URL********************"+request.getRequestURL().toString());
		System.out.println("请求URI********************"+request.getRequestURI().toString());
		String no = 	(String) request.getAttribute("order_no");
		String price = 	(String) request.getAttribute("price");
		String body = 	(String) request.getAttribute("body");
		if(type==1){//余额充值
			String url = "pay/rechargeAliPayNotify";
			return payService.aliPrePay(no, price ,body, request,url);
		}else if(type ==2){//兑换券充值
			String url = "pay/ticketAliPayNotify";
			return payService.aliPrePay(no, price ,body, request,url);
		}else if(type ==3){//订单支付
			String url = "pay/OrderAliPayNotify";
			return payService.aliPrePay(no, price ,body, request,url);
		}else if(type ==4){//套餐购买
			String url = "pay/packageAliPayNotify";
			return payService.aliPrePay(no, price ,body, request,url);
		}
		
		return null;
	}
	
	/**
	 * 支付宝支付结果查询接口
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/aliQuery")
	public Object aliQuery(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, String out_trade_no)
			throws Exception {
		AlipayClient alipayClient = new DefaultAlipayClient(ConfigUtil.ALIPAY_URL,
				ConfigUtil.ALIPAY_APP_ID, ConfigUtil.ALIPAY_APP_PRIVATE_KEY, "json", ConfigUtil.ALIPAY_CHARSET,
				ConfigUtil.ALIPAY_PUBLIC_KEY, "RSA2");
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		request.setBizContent("{" + "\"out_trade_no\":\""+out_trade_no+"\"" +
				"}");
		String result =  alipayClient.execute(request).getBody();
		return JSON.parse(result);
	}
	
}
